import { App, createApp } from "vue";
import { createPinia } from "pinia";
import "./style.css";
import App_ from "@/App.vue";
// import ElementPlus from "element-plus";
// import {install} from "@/components/toast/useToast"

import router from "./routers";
import i18n from "./locals";
import axios from "axios";
import { createVuetify } from "vuetify";
import * as components from "vuetify/components";
import * as directives from "vuetify/directives";
import { aliases, mdi } from "vuetify/iconsets/mdi";
// import Vue3Lottie from "vue3-lottie";
import { get, post } from "@/comm/request";
import VueSocialSharing from "vue-social-sharing";

import "vuetify/styles";
import "@mdi/font/css/materialdesignicons.css";
import "./styles/variables.scss";
import { Conn } from "@/connetion/Conn";
import { URL_GOOGLE_CALLBACK } from "./comm/api";
import { getURLParams } from "./comm/tooler";
import { toast } from "./components/toast/toast";
import { Core } from "./comm/Core";
import { Bus } from "./connetion/Bus";
import { getTodayYesterdayPrice, getPriceByTime } from "./comm/web3";


// import newEn from "@/locals/langs/en-0"
// import en from "@/locals/langs/en"

// setTimeout(() => {
//     console.log(newEn, "newEn--------------")
//     console.log(en, "en--------------")
//     let t = Object.assign(en, newEn);
//     console.log(t)
// }, 4000);

if (location.search.includes("logs=1")) {
    localStorage.setItem("logs", "1");
    logs();
}

function checkMobile() {
    return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
}
if (checkMobile()) {
    // location.href = "https://baidu.com"
    if (location.host == "alpha.chainingview.io") {
        location.href = location.href.replace("//alpha.", "//m.");
    }
}

// alert(navigator.userAgent)

function logs() {
    if (localStorage.getItem("logs") == "1") {
        var script = document.createElement("script");
        script.src = "https://cdn.jsdelivr.net/npm/eruda";
        document.body.append(script);
        script.onload = function () {
            (window as any).eruda.init();
        };
        localStorage.removeItem("logs");
    }
}


if (location.href.indexOf("/oauth/google") !== -1) {
    let param = extractValuesFromURL(location.href);
    console.log("param", param);
    if (param) {
        location.replace(location.origin + "?state=" + param.state + "&access_token=" + param.access_token);
    }
}

function extractValuesFromURL(url: string) {
    let obj = url.match(/state=([^&]+)&access_token=([^&]+)/);
    if (obj?.length == 3) {
        return {
            state: obj[1],
            access_token: obj[2]
        };
    }
    return null;
}

let app: App;

// if (!Core.ins.checkThirdCallbak()) {
const vuetify = createVuetify({
    components,
    directives,
    theme: {
        defaultTheme: "dark",
        themes: {
            light: {
                colors: {
                    primary: "#1867C0",
                    secondary: "#5CBBF6"
                }
            },
            dark: {
                colors: {
                    primary: "#fcc235",
                    secondary: "#5CBBF6"
                }
            }
        }
    },
    icons: {
        defaultSet: "mdi",
        aliases,
        sets: {
            mdi
        }
    }
});


const store = createPinia();
app = createApp(App_);
app.use(router);
app.use(store);
app.use(vuetify);
app.use(i18n);
// app.use(Vue3Lottie);
app.use(VueSocialSharing);
app.mount("#app");
// } else {
//     console.log("第三方回调地址");
// }
// export { app };
// setTimeout(async () => {
//     let res = await getTodayYesterdayPrice(false, ["0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"], Date.now() - 30 * 60 * 1000);
//     console.log("TodayYesterdayPrice", res);
//     // res = await getPriceByTime(["0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"], Date.now() - 10 * 60 * 1000);
//     // console.log("NowPrice", res);
// }, 3000);

(window as any).changeIcon = changeIcon;
function changeIcon() {
    let doc: any = window.document;
    var dom: any = doc.querySelector("body > w3m-modal")?.shadowRoot?.querySelector("wui-flex > wui-card > w3m-router").shadowRoot.querySelector("div > w3m-connecting-siwe-view")?.shadowRoot?.querySelector("wui-flex:nth-child(1) > w3m-connecting-siwe")?.shadowRoot.querySelector("wui-visual-thumbnail:nth-child(1)")?.shadowRoot;
    if (dom) {
        //判断是否已经替换过图片
        console.log('替换内容！！', dom.innerHTML)
        if (dom.innerHTML == '<img src="/asset/imgs/ChainingViewLogo.png" style="width:100%;height:100%">') {
            return true;
        } else {
            dom.innerHTML = "<img src='/asset/imgs/ChainingViewLogo.png' style='width:100%;height:100%'/>";
            var v1 = doc.querySelector("body > w3m-modal").shadowRoot.querySelector("wui-flex > wui-card > w3m-router").shadowRoot.querySelector("div > w3m-connecting-siwe-view").shadowRoot.querySelector("wui-flex:nth-child(2) > wui-text");
            v1.innerHTML = "ChainingView needs to connect to your wallet"
        }
    }
    return false;
    // console.log('改变图片的dom!!', dom)
}
export {
    store
}